package com.gbei.center.oauth.service.impl;

import com.gbei.center.model.OpenResource;
import com.gbei.center.model.OpenResourceApp;
import com.gbei.center.oauth.mapper.AuthAccessTokenMapper;
import com.gbei.center.oauth.mapper.OpenResourceAppMapper;
import com.gbei.center.oauth.mapper.OpenResourceMapper;
import com.gbei.center.oauth.model.AuthAccessToken;
import com.gbei.center.oauth.req.*;
import com.gbei.center.oauth.service.ApiService;
import com.gbei.center.oauth.service.WaterMachineService;
import com.gbei.center.req.open.ReqAreaCode;
import com.gbei.center.req.open.ReqThirdpartyPlaceOrder;
import com.gbei.center.req.open.ReqUserCommon;
import com.gbei.center.utils.ArrayUtil;
import com.gbei.center.utils.JsonUtils;
import com.gbei.center.utils.RestTemplateClient;
import com.gbei.center.utils.message.ResponseInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.util.*;

/**
 * @Discription 水机相关
 * @Author yang jun
 * @Date 2019/5/23 11:58
 */
@Service
public class WaterMachineServiceImpl implements WaterMachineService {

    @Autowired
    private OpenResourceAppMapper openResourceAppMapper;
    @Autowired
    private OpenResourceMapper openResourceMapper;
    @Autowired
    private RestTemplateClient restTemplateClient;
    @Autowired
    private AuthAccessTokenMapper authAccessTokenMapper;


    /**
     * 设置区县运营商
     * @param req
     * @return
     */
    @Override
    public String addOperator(ReqAddOperator req,String apiUrl) {

        //根据token查询记录
        AuthAccessToken authAccessToken = authAccessTokenMapper.selectByAccessToken(req.getToken());
        String openAppId = authAccessToken.getClientId();
        req.setOpenAppId(openAppId);
        //判断接口权限
        if (!checkApiScope(apiUrl,openAppId)){
            return JsonUtils.toJson(ResponseInfo.LACK_API_SCOPE.messageToResult());
        }

        //调用第三方平台接口
        String result = restTemplateClient.common(req,"http://settlement/waterMachineSecond/addOperator");
        return result;
    }


    /**
     * 删除区县运营商
     * @param req
     * @return
     */
    @Override
    public String deleteOperator(ReqDeleteOperator req, String apiUrl) {

        //根据token查询记录
        AuthAccessToken authAccessToken = authAccessTokenMapper.selectByAccessToken(req.getToken());
        String openAppId = authAccessToken.getClientId();
        req.setOpenAppId(openAppId);
        //判断接口权限
        if (!checkApiScope(apiUrl,openAppId)){
            return JsonUtils.toJson(ResponseInfo.LACK_API_SCOPE.messageToResult());
        }

        //调用第三方平台接口
        String result = restTemplateClient.common(req,"http://settlement/waterMachineSecond/deleteOperator");
        return result;
    }


    /**
     * 添加区县商户
     * @param req
     * @return
     */
    @Override
    public String addMerchant(ReqAddMerchant req, String apiUrl) {

        //根据token查询记录
        AuthAccessToken authAccessToken = authAccessTokenMapper.selectByAccessToken(req.getToken());
        String openAppId = authAccessToken.getClientId();
        req.setOpenAppId(openAppId);
        //判断接口权限
        if (!checkApiScope(apiUrl,openAppId)){
            return JsonUtils.toJson(ResponseInfo.LACK_API_SCOPE.messageToResult());
        }

        //调用第三方平台接口
        String result = restTemplateClient.common(req,"http://settlement/waterMachineSecond/addMerchant");
        return result;
    }


    /**
     * 删除区县商户
     * @param req
     * @return
     */
    @Override
    public String deleteMerchant(ReqDeleteMerchant req, String apiUrl) {

        //根据token查询记录
        AuthAccessToken authAccessToken = authAccessTokenMapper.selectByAccessToken(req.getToken());
        String openAppId = authAccessToken.getClientId();
        req.setOpenAppId(openAppId);
        //判断接口权限
        if (!checkApiScope(apiUrl,openAppId)){
            return JsonUtils.toJson(ResponseInfo.LACK_API_SCOPE.messageToResult());
        }

        //调用第三方平台接口
        String result = restTemplateClient.common(req,"http://settlement/waterMachineSecond/deleteMerchant");
        return result;
    }


    /**
     * 下单
     * @param req
     * @return
     */
    @Override
    public String placeOrder(ReqPlaceOrder req, String apiUrl) {

        //根据token查询记录
        AuthAccessToken authAccessToken = authAccessTokenMapper.selectByAccessToken(req.getToken());
        String openAppId = authAccessToken.getClientId();
        req.setOpenAppId(openAppId);
        //判断接口权限
        if (!checkApiScope(apiUrl,openAppId)){
            return JsonUtils.toJson(ResponseInfo.LACK_API_SCOPE.messageToResult());
        }

        //调用第三方平台接口
        String result = restTemplateClient.common(req,"http://settlement/waterMachineSecond/placeOrder");
        return result;
    }


    /**
     * 判断应用是否有这个api的权限
     * @param apiUrl
     * @param openAppId
     * @return
     */
    public boolean checkApiScope(String apiUrl,String openAppId){
        OpenResource openResource = openResourceMapper.getOpenResourceByName(apiUrl);
        if (openResource != null){
            OpenResourceApp openResourceApp = openResourceAppMapper.getOpenResourceApp(openAppId, openResource.getResourceId());
            if (openResourceApp != null){
                return true;
            }
        }
        return false;
    }


}
